package com.action.addrbook;

import com.common.*;
import com.db.addrbook.*;
import com.db.user.UserVO;
import com.opensymphony.xwork2.ActionContext;
import com.sun.org.apache.bcel.internal.generic.NEW;
import java.util.*;

public class AddrOutputAction {
	private int type = 1;
	private int addrno;
	private String abAction;
	
	private String fword;
	private String sword;	
	private AddrBookVO abvo = new AddrBookVO();
	private List<Object> ablist;
	private List<CategoryVO> calist;
	
	private int curpage = 1;
	private int totalpage;
	private int lpp = 10;	
	
	private AddrBookDAO abDAO;
	private CategoryDAO caDAO;
	private ListProcess listProc;	
	private DateProcess dateProc;

	
	public String getAbAction() {
		return abAction;
	}
	// 검색 목록 출력시 페이지 구분을 위해 검색어를 별도로 저장할 수 있도록 JSP로 넘겨준다
	public String getFword() {
		return fword;
	}
	public String getSword() {
		return sword;
	}
	// DAO 및 Process클래스들을 Spring으로부터 받아온다
	public void setListProc(ListProcess listProc) {
		this.listProc = listProc;
	}
	public void setDateProc(DateProcess dateProc) {
		this.dateProc = dateProc;
	}
	public void setAbDAO(AddrBookDAO abDAO) {
		this.abDAO = abDAO;
	}
	public void setCaDAO(CategoryDAO caDAO) {
		this.caDAO = caDAO;
	}
	public List<CategoryVO> getCalist() {
		return calist;
	}
	public void setFword(String fword) {
		this.fword = fword;
	}
	public void setSword(String sword) {
		this.sword = sword;
	}
	public int getCurpage() {
		return curpage;
	}
	public void setCurpage(int curpage) {
		this.curpage = curpage;
	}
	public int getTotalpage() {
		return totalpage;
	}	
	public void setAddrno(int addrno) {
		this.addrno = addrno;
	}
	public void setType(int type) {
		this.type = type;
	}	
	public int getType() {
		return type;
	}
	public AddrBookVO getAbvo() {
		return abvo;
	}
	public List<Object> getAblist() {
		return ablist;
	}
	
	public String getABContent(){
		String result = ""; 		
		int sabun = getSessionSabun();
		try {
			abvo = abDAO.getAddrBookContent(addrno);
			// System.out.println(abvo.toString());
			// 생일 처리 (select태그의 option값으로 출력을 용이하게 하기위해 값들을 분리해 넘긴다)
			String bdArr[] = dateProc.dateToValue(abvo.getBirth());
			abvo.setBdyear(bdArr[0]);
			abvo.setBdmonth(bdArr[1]);
			abvo.setBdday(bdArr[2]);
			int ctype = caDAO.getCateType(abvo.getCno());
			calist = caDAO.getCategoryList(ctype);
		} catch (Exception e) {
			System.out.println("AddrOutputAction.getABContent():"+e.toString());
		}		
		return result;		
	}	

	public String getABList(){
		String result = "";				
		try {
			Map<String, Integer> inmap = new HashMap<String, Integer>();			
			inmap.put("type", new Integer(type));
			int sabun = getSessionSabun();
			inmap.put("msabun", new Integer(sabun));
			inmap.put("curpage", new Integer(curpage));
			inmap.put("lpp", new Integer(lpp));						
			ablist = abDAO.getAddrBookList(inmap);
			// 총페이지 수를 얻어오는 부분
			// 타입(개인, 부서) 과 사번(주소록 주인)을 vo타입으로 묶어서 인자값으로 보낸다			
			abvo.setType(type);
			abvo.setMsabun(sabun);			
			totalpage = abDAO.getTotalPage(abvo, lpp);
			ablist = listProc.createPage(curpage, lpp, totalpage, ablist);
			abAction = "list";
			result = "success";
		} catch (Exception e) {
			System.out.println("AddrOutputAction.getABList():"+e.toString());
			result = "error";
		}		
		return result;
	}
	
	public String getABFind(){
		String result = "";			
		try {
			Map<String, Object> inmap = new HashMap<String, Object>();
			inmap.put("type", new Integer(type));
			int sabun = getSessionSabun();
			inmap.put("msabun", new Integer(sabun));			
			inmap.put("lpp", new Integer(lpp));			
			inmap.put("fword", fword);
			inmap.put("sword", sword);
			//System.out.println(fword+"|"+sword);
			ablist = abDAO.getAddrBookFind(inmap);
			// 페이지 구별
			totalpage = listProc.getTotalPage(ablist, lpp);
			ablist = listProc.createPage(curpage, lpp, totalpage, ablist);
			abAction = "find";
			result = "success";
		} catch (Exception e) {
			System.out.println("AddrOutputAction.getABContent():"+e.toString());
			result = "error";
		}		
		return result;		
	}
	
	public int getSessionSabun(){
		ActionContext context = ActionContext.getContext();
		Map<String, UserVO> userMap = context.getSession();
		UserVO vo = userMap.get("user");
		int sabun = vo.getSabun();
		return sabun;
	}
}
